﻿using System;
using System.Configuration;
using System.Web.UI.HtmlControls;
using DotNetNuke.Entities.Modules;
using EntitySpaces.Interfaces;

namespace DNNspot.LiveChat
{
    public class ModuleBase : PortalModuleBase
    {
        // ### EVENTS ######################################################

        protected void Page_Init(Object sender, EventArgs e)
        {
            InitializeProvider();
        }

        // ### PRIVATE FUNCTIONS ###########################################

        private static void InitializeProvider()
        {
            if (esConfigSettings.ConnectionInfo.Default != "SiteSqlServer")
            {
                esConfigSettings connectionInfoSettings = esConfigSettings.ConnectionInfo;
                foreach (esConnectionElement connection in connectionInfoSettings.Connections)
                {
                    //if there is a SiteSqlServer in es connections set it default
                    if (connection.Name == "SiteSqlServer")
                    {
                        esConfigSettings.ConnectionInfo.Default = connection.Name;
                        return;
                    }
                }

                //no SiteSqlServer found grab dnn cnn string and create
                string dnnConnection = ConfigurationManager.ConnectionStrings["SiteSqlServer"].ConnectionString;

                // Manually register a connection
                esConnectionElement conn = new esConnectionElement();
                conn.ConnectionString = dnnConnection;
                conn.Name = "SiteSqlServer";
                conn.Provider = "EntitySpaces.SqlClientProvider";
                conn.ProviderClass = "DataProvider";
                conn.SqlAccessType = esSqlAccessType.DynamicSQL;
                conn.ProviderMetadataKey = "esDefault";
                conn.DatabaseVersion = "2005";

                // Assign the Default Connection
                esConfigSettings.ConnectionInfo.Connections.Add(conn);
                esConfigSettings.ConnectionInfo.Default = "SiteSqlServer";

                // Register the Loader
                esProviderFactory.Factory = new EntitySpaces.LoaderMT.esDataProviderFactory();
            }
        }

        // ### PROTECTED FUNCTIONS #########################################

        protected bool IsAuthenticatedEditor()
        {
            foreach (var role in ModuleConfiguration.AuthorizedEditRoles.Split(','))
                if (UserInfo.IsInRole(role)) return true;

            return false;
        }

        protected void RegisterJavascript(string fullPath)
        {
            var script = new HtmlGenericControl("script");
            script.Attributes.Add("type", "text/javascript");
            script.Attributes.Add("src", fullPath);

            Page.Header.Controls.Add(script);
        }

        protected string ModuleWebPath
        {
            get
            {
                return String.Format("/DesktopModules/{0}/", ModuleConfiguration.FolderName);
            }
        }
    }
}
